#include "yearmonthwidget.h"
#include <QDate>
#include <QHeaderView>
#include <QStandardItem>
#include <ui_YearMonthWidget.h>
#define CELL_HEIGHT 30
#define CELL_WIDTH 46

YearMonthWidget::YearMonthWidget(QWidget* parent) : QWidget(parent), ui(new Ui::YearMonthWidget), m_maxYear("9989"), m_minYear("1748"), m_rowCount(5), m_columnCount(2), m_dateType(0)
{
	ui->setupUi(this);
	m_pDateModel = new QStandardItemModel(m_rowCount, m_columnCount, this);
	ui->tableView->setModel(m_pDateModel);
	initUI();
	m_currentYear = QDate::currentDate().year();
	m_currentPage = 0;

	updateYearModelData();

	m_months = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一", "十二"};
}

YearMonthWidget::~YearMonthWidget()
{
	delete ui;
}

void YearMonthWidget::showYearMonthWidget(int type)
{
	m_dateType = type;
	ui->buttonWidget->setVisible(m_dateType != 0);
	int totalHeight;
	if (m_dateType == 0)
	{
		m_rowCount = 6;
		m_pDateModel->setRowCount(m_rowCount);
		updateMonthModelData();
		totalHeight = ui->tableView->height() + 2;
	}
	else
	{
		m_rowCount = 5;
		m_pDateModel->setRowCount(m_rowCount);
		m_currentPage = 0;
		m_currentYear = QDate::currentDate().year();
		updateYearModelData();
		totalHeight = ui->tableView->height() + ui->buttonWidget->height();
	}

	setFixedHeight(totalHeight);
	show();
}

QString YearMonthWidget::convertMonthText(int month)
{
	if (month >= 0 && month <= m_months.size())
	{
		return m_months.at(month - 1);
	}
	return "";
}

void YearMonthWidget::on_prePushButton_clicked()
{
	m_currentPage--;
	updateYearModelData();
}

void YearMonthWidget::on_closePushButton_clicked()
{
	this->close();
}

void YearMonthWidget::on_nextPushButton_clicked()
{
	m_currentPage++;
	updateYearModelData();
}

void YearMonthWidget::initUI()
{
	setWindowFlags(/*Qt::WindowStaysOnTopHint | */ Qt::FramelessWindowHint /*| Qt::SubWindow*/ | Qt::Popup);

	//    setAttribute(Qt::WA_TranslucentBackground);
	ui->tableView->verticalHeader()->hide();
	ui->tableView->horizontalHeader()->hide();
	ui->tableView->setShowGrid(false);
	ui->tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	ui->tableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
	//    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自动设置行高
	connect(ui->tableView, &QTableView::clicked, this, [&](const QModelIndex& index) {
		if (index.isValid())
		{
			QString dataStr = index.data().toString();

			int newValue;
			if (m_dateType == 0)
			{
				newValue = m_months.indexOf(dataStr);
				if (newValue < 0)
				{
					newValue = 1;
				}
				else
				{
					newValue = newValue + 1;
				}
			}
			else
			{
				newValue = dataStr.toUInt();
			}
			emit yearMonthButtonIsclicked(m_dateType, newValue);
			this->close();
		}
	});


	for (int wValue = 0; wValue < m_columnCount; wValue++)
	{
		ui->tableView->setColumnWidth(wValue, CELL_WIDTH);
	}

	for (int hValue = 0; hValue < m_rowCount; hValue++)
	{
		ui->tableView->setRowHeight(hValue, CELL_HEIGHT);
	}

	setFixedWidth(CELL_WIDTH * m_columnCount + 2);
}

void YearMonthWidget::updateYearModelData()
{
	m_leftTopYear = QString::number(m_currentYear - m_rowCount + m_currentPage * (m_rowCount * m_columnCount));

	if (m_leftTopYear == m_minYear)
	{
		ui->prePushButton->setEnabled(false);
	}
	else
	{
		ui->prePushButton->setEnabled(true);
	}

	if (m_leftTopYear == m_maxYear)
	{
		ui->nextPushButton->setEnabled(false);
	}
	else
	{
		ui->nextPushButton->setEnabled(true);
	}

	// 填充数据
	for (int column = 0; column < m_columnCount; ++column)
	{
		for (int row = 0; row < m_rowCount; ++row)
		{
			QString text = QString::number(m_currentYear - m_rowCount + row + m_rowCount * column + m_currentPage * (m_rowCount * m_columnCount));
			QStandardItem* item = m_pDateModel->item(row, column);
			if (!item)
			{
				item = new QStandardItem(text);
			}
			else
			{
				item->setText(text);
			}

			item->setTextAlignment(Qt::AlignCenter);

			m_pDateModel->setItem(row, column, item);
		}
	}

	ui->tableView->setFixedHeight(CELL_HEIGHT * m_rowCount);
}

void YearMonthWidget::updateMonthModelData()
{
	// 填充数据
	for (int column = 0; column < m_columnCount; ++column)
	{
		for (int row = 0; row < m_rowCount; ++row)
		{
			QString text = m_months.at(column * m_rowCount + row);
			QStandardItem* item = m_pDateModel->item(row, column);
			if (!item)
			{
				item = new QStandardItem(text);
			}
			else
			{
				item->setText(text);
			}

			item->setTextAlignment(Qt::AlignCenter);

			m_pDateModel->setItem(row, column, item);
		}
	}

	ui->tableView->setFixedHeight(CELL_HEIGHT * m_rowCount);
}
